package com.cw.java8.builtInFunction;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

/**
 * 函数演示
 *
 */
public class MyFunction {

    public static void main(String[] args) {

        // R apply(T t);
        Function<String, Integer> function = (String t) -> Integer.valueOf(t);
        Integer ii = function.apply("123");
        System.out.println(ii);


        List<Double> list = Arrays.asList(58.5D, 34D, 68D, 98D, 65D, 78D, 89D, 58D, 96D);
        List<Double> resultList = wapperList(list, (Double score) -> {
            return score + 5;
        });

        for (Double score : resultList) {
            System.out.print(score + "  ");
        }
    }

    /**
     * 利用一个函数对list数据施加操作
     *
     * @param list
     * @param function
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> List<R> wapperList(List<T> list, Function<T, R> function) {
        //结果List
        List<R> resultList = new LinkedList<>();

        //循环全班成绩的List
        for (T t : list) {
            //每个人加5分
            resultList.add(function.apply(t));
        }
        return resultList;
    }
}
